package com.eshop.modules.wechat.rest;

import java.lang.invoke.SerializedLambda;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.modules.aop.ForbidSubmit;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.common.error.WxErrorException;
import com.eshop.exception.BadRequestException;
import com.eshop.utils.OrderUtil;
import com.eshop.modules.mp.config.WxMpConfiguration;
import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import com.eshop.modules.mp.domain.WechatMenu;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.http.ResponseEntity;
import com.eshop.modules.mp.service.WechatMenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:微信菜單" })
@RestController
@RequestMapping({ "api" })
public class WechatMenuController
{
    private final WechatMenuService WechatMenuService;
    
    public WechatMenuController(final WechatMenuService WechatMenuService) {
        this.WechatMenuService = WechatMenuService;
    }
    
    @ApiOperation("查询菜单")
    @GetMapping({ "/YxWechatMenu" })
    @PreAuthorize("hasAnyRole('admin','YxWechatMenu_ALL','YxWechatMenu_SELECT')")
    public ResponseEntity getYxWechatMenus() {
        return new ResponseEntity(this.WechatMenuService.getOne(Wrappers.<WechatMenu>lambdaQuery().eq(WechatMenu::getKey, "wechat_menus")), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @ApiOperation("创建菜单")
    @PostMapping({ "/YxWechatMenu" })
    @PreAuthorize("hasAnyRole('admin','YxWechatMenu_ALL','YxWechatMenu_CREATE')")
    public ResponseEntity create(@RequestBody final String jsonStr) {
        final JSONObject jsonObject = JSON.parseObject(jsonStr);
        final String jsonButton = jsonObject.get("buttons").toString();
        final WechatMenu WechatMenu = new WechatMenu();
        final Boolean isExist = this.WechatMenuService.isExist("wechat_menus");
        final WxMenu menu = (WxMenu)JSONObject.parseObject(jsonStr, (Class)WxMenu.class);
        final WxMpService wxService = WxMpConfiguration.getWxMpService();
        if (isExist) {
            WechatMenu.setKey("wechat_menus");
            WechatMenu.setResult(jsonButton);
            this.WechatMenuService.saveOrUpdate(WechatMenu);
        }
        else {
            WechatMenu.setKey("wechat_menus");
            WechatMenu.setResult(jsonButton);
            WechatMenu.setAddTime(Integer.valueOf(OrderUtil.getSecondTimestampTwo()));
            this.WechatMenuService.save(WechatMenu);
        }
        try {
            wxService.getMenuService().menuDelete();
            wxService.getMenuService().menuCreate(menu);
        }
        catch (WxErrorException e) {
            throw new BadRequestException(e.getMessage());
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}
